package com.ruoyi.web.controller.charge;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.charge.domain.Charger;
import com.ruoyi.charge.service.IChargerService;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.framework.annotation.RyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.Validated;

import java.util.Arrays;
import java.util.List;

/**
 * 充电桩 操作处理
 *
 * @author laifeng
 */
@Controller
@Mapping("charger")
@Api("充电桩管理")
public class ChargerController extends BaseController {

    @Inject
    IChargerService chargerService;

    /**
     * 查询充电桩列表
     */
    @AuthPermissions("charge:charger:list")
    @Get
    @Mapping("list")
    @ApiOperation("充电桩列表")
    public PageData<Charger> list(Page<Charger> page, Charger charger) {
        QueryWrapper qw = getQW(charger);
        Page<Charger> result = chargerService.page(page, qw);
        return getPageData(result);
    }

    /**
     * 导出充电桩列表
     */
    @RyLog(title = "充电桩", businessType = BusinessType.EXPORT)
    @AuthPermissions("charge:charger:export")
    @Post
    @Mapping("export")
    @ApiOperation("导出充电桩列表")
    public void export(Charger charger) {
        QueryWrapper qw = getQW(charger);
        List<Charger> list = chargerService.list(qw);
        ExcelUtil.export(list);
    }

    /**
     * 查询充电桩详情
     */
    @AuthPermissions("charge:charger:query")
    @Get
    @Mapping("{id}")
    @ApiOperation("查询充电桩详情")
    public Charger info(@Path Long id) {
        return chargerService.getById(id);
    }

    /**
     * 新增充电桩
     */
    @RyLog(title = "充电桩", businessType = BusinessType.INSERT)
    @AuthPermissions("charge:charger:add")
    @Post
    @Mapping
    @ApiOperation("新增充电桩")
    public Result<Void> add(@Body @Validated Charger charger) {
        return toResult(chargerService.save(charger));
    }

    /**
     * 修改充电桩
     */
    @RyLog(title = "充电桩", businessType = BusinessType.UPDATE)
    @AuthPermissions("charge:charger:edit")
    @Put
    @Mapping
    @ApiOperation("修改充电桩")
    public Result<Void> edit(@Body @Validated Charger charger) {
        return toResult(chargerService.updateById(charger));
    }

    /**
     * 删除充电桩
     */
    @RyLog(title = "充电桩", businessType = BusinessType.DELETE)
    @AuthPermissions("charge:charger:remove")
    @Delete
    @Mapping("{ids}")
    @ApiOperation("删除充电桩")
    public Result<Void> delete(@Path Long[] ids) {
        return toResult(chargerService.removeByIds(Arrays.asList(ids)));
    }

    private QueryWrapper getQW(Charger charger) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(Charger::getSn).eq(charger.getSn());
        qw.and(Charger::getGunNum).eq(charger.getGunNum());
        qw.and(Charger::getType).eq(charger.getType());
        qw.and(Charger::getServerIp).eq(charger.getServerIp());
        qw.and(Charger::getServerPort).eq(charger.getServerPort());
        qw.and(Charger::getBrand).eq(charger.getBrand());
        return qw;
    }

}
